package com.example.democlient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.democlient.network.SocketService;
import com.example.democlient.network.serverpackets.SM_USER_REGISTER_REQUEST;

public class RegisterActivity extends Activity implements OnClickListener {
	private Button register;
	private EditText regAccountEditText;
	private EditText regAccountNameEditText;
	private EditText regPassword;
	private EditText regPasswordConfirm;
	private EditText company;
	private MyApplication app;
	Handler mainHandler = new Handler() {  
        @Override  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 1:  
            	Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "注册成功.",Toast.LENGTH_LONG).show();
            	Intent toIntent = new Intent();
    			toIntent.setClass(RegisterActivity.this,
    			TaskActivity.class);
    			startActivity(toIntent);
            break;  
            default:  
            	Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "注册失败.",Toast.LENGTH_LONG).show();
                break;  
            }  
            super.handleMessage(msg);  
        }  
    };  
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        register = (Button)findViewById(R.id.regRegister_button);
        register.setOnClickListener(this);
        regAccountEditText = (EditText)findViewById(R.id.regAccountEditText);
        regAccountNameEditText = (EditText)findViewById(R.id.regAccountNameEditText);
        regPassword =(EditText)findViewById(R.id.regPassword);
        regPasswordConfirm=(EditText)findViewById(R.id.regPasswordConfirm);
        company=(EditText)findViewById(R.id.regCompany);
        app = (MyApplication)this.getApplication();
        app.addActivity(this);
        app.setHandler(this.getClass(),mainHandler);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	if(item.getItemId()==R.id.menu_settings){
    		app.exit();
    	}
		return true;
    }
    @Override
	public void onStart(){
		super.onStart();
		app = (MyApplication)this.getApplication();
        app.setHandler(this.getClass(),mainHandler);
	}
	@Override
	public void onClick(View arg0) {
		if(arg0.getId()==R.id.regRegister_button ){
			String regAccountEditTextStr = regAccountEditText.getText().toString();
			if(regAccountEditTextStr.equals("")){
				Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "请输入账号.",Toast.LENGTH_LONG).show();
				return;
			}
			String regAccountNameEditTextStr = regAccountNameEditText.getText().toString();
			if(regAccountNameEditTextStr.equals("")){
				Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "请输入用户名.",Toast.LENGTH_LONG).show();
				return;
			}
			String password = regPassword.getText().toString();
			if("".equals(password)||!password.equals(regPasswordConfirm.getText().toString())){
				Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "两次密码不一致.",Toast.LENGTH_LONG).show();
				return;
			}
			String companyStr = company.getText().toString();
			if(companyStr.equals("")){
				Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "请输入单位名称.",Toast.LENGTH_LONG).show();
				return;
			}
			if(SocketService.getInstance().sendPacket(new SM_USER_REGISTER_REQUEST(regAccountEditTextStr,password,regAccountNameEditTextStr,companyStr), app)){
				
			}
		}
	}

    
}
